import DemensionReduce.LLEClass as LLE
import numpy as np
import pandas as pd
import datetime
from sklearn import preprocessing
from dataPreprocess import dataPreprocessing
from scipy.spatial.distance import cdist
from scipy.stats import spearmanr
from sklearn.neighbors import NearestNeighbors

import kDimensionReduce as RD # 确定外部添加路径后，kDimensionReduce 是Dll/So的名字

# 完整测试功能
def Test_Complete(data):

    print("Total Data", data.shape[0], "X", data.shape[1])

    datetime1 = datetime.datetime.now()

    #
    step = 5
    min_k = step
    max_k = data.shape[0] - step
    optimal_k = RD.LLE_Find_K(data, step, min_k, max_k, "spearman", False)
    print("Optimal K", optimal_k)
    datetime2 = datetime.datetime.now()
    print("Time Consume", (datetime2 - datetime1).total_seconds())

    #
    optimal_m = RD.LLE_Find_M(data, optimal_k, 0.95,  False)
    print("Optimal M", optimal_m)
    datetime3 = datetime.datetime.now()
    print("Time Consume", (datetime3 - datetime2).total_seconds())

    #
    y_data = RD.LLE_Run(data, optimal_k, optimal_m, False, "Test Debug Flag")
    datetime4 = datetime.datetime.now()
    print("Time Consume LLE", (datetime4 - datetime3).total_seconds())
    print("Time Consume", (datetime4 - datetime1).total_seconds())
    # print(y_data)


# 批量测试
def Porformance_Test(data):
    for i in range(1):
        Test_Complete(data)

# 简单测试是否跑通
def Test_once(data):

    # 测试DLL是否正常工作
    print(RD.TestPYCPPConnect(4, 3))

    # 参数1 数据 ndarray（2维）
    # 参数2 步长
    # 参数3 起始k
    # 参数4 最大k
    # 参数5 相关性计算方式 spearman / pearson
    # 参数6 是否输出Debug信息
    optimal_k = RD.LLE_Find_K(data, 1, 2, 9, "spearman", False)
    print("Optimal K", optimal_k)

    # 参数1 数据 ndarray（2维）
    # 参数2 k 邻居数值
    # 参数3 v 最小信息量
    # 参数4 是否输出Debug信息
    optimal_m = RD.LLE_Find_M(data, 5, 0.95,  False)
    print("Optimal M", optimal_m)

    # 参数1 数据 ndarray（2维）
    # 参数2 k 邻居数值
    # 参数3 m 降维成分维度
    # 参数4 是否输出Debug信息
    # 参数5 自定义参数
    y_data = RD.LLE_Run(data, 5, 2, False, "Test Debug Flag")
    print(y_data)


if __name__ == '__main__':
    # data = pd.read_csv(r'E:\智投\主观矩阵\2019年\12月\W5\FCST_HS300.csv')
    data = pd.read_csv("C:\\Users\\fengshimeng3\\Documents\\财富管理-智能投顾\\Data\\FCST_HS300-0607.csv")
    # data = data.iloc[:50, :15]
    # data = data.iloc[:200, :10]
    # print(data)
    # data = pd.read_csv(r'FCST_HS300.csv')
    data.rename(columns={'Unnamed: 0':'Date'},inplace=True)
    d = dataPreprocessing('Z_HS300', 'Date')
    dat = d.dataCleaning(data)
    X, Y = d.dataSplit(dat)

    data = np.matrix(X)
    data = np.matrix(preprocessing.scale(data))

    # 随机生成数据进行测试
    # data = np.random.rand(10, 3)

    print(data)
    # df_test_data = pd.DataFrame(data)
    # df_test_data.to_csv("d://rd_test_10sample.csv")

    # 测试函数
    Test_once(data)
    Porformance_Test(data)